﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YJKFamilyLibDataManager;

namespace YJKFamilyLibManager.FamilyLibModule
{
	public partial class DlgCheck : System.Windows.Forms.Form
	{
		public List<CategoryForSelect> CategoryList
		{
			get
			{
				List<CategoryForSelect> list = new List<CategoryForSelect>();
				this.getChildNodes(this.tvCategory.Nodes[0], list);
				return list;
			}
		}

		private void getChildNodes(TreeNode node, List<CategoryForSelect> categoryList)
		{
			foreach (object obj in node.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.Tag is CategoryForSelect)
				{
					categoryList.Add((CategoryForSelect)treeNode.Tag);
				}
				foreach (object obj2 in treeNode.Nodes)
				{
					TreeNode treeNode2 = (TreeNode)obj2;
					if (treeNode2.Tag is CategoryForSelect)
					{
						categoryList.Add((CategoryForSelect)treeNode2.Tag);
					}
					this.getChildNodes(treeNode2, categoryList);
				}
			}
		}

		public DlgCheck(List<FamilyLibDatabase> dbs)
		{
			this.mDbs = dbs;
			this.InitializeComponent();
			this.cbxBb.Items.Clear();
			foreach (FamilyLibDatabase item in this.mDbs)
			{
				this.cbxBb.Items.Add(item);
			}
			if (this.cbxBb.Items.Count > 0)
			{
				this.cbxBb.SelectedIndex = 0;
			}
		}

		private void DlgCompare_Load(object sender, EventArgs e)
		{
		}

		private void tvCategory_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (this.tvCategory.SelectedNode.Tag is CategoryForSelect)
			{
				this.lbxFamily.Items.Clear();
				CategoryForSelect categoryForSelect = this.tvCategory.SelectedNode.Tag as CategoryForSelect;
				foreach (FamilyForSelect familyForSelect in categoryForSelect.FamilyList)
				{
					this.lbxFamily.Items.Add(familyForSelect, familyForSelect.Selected);
				}
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			foreach (CategoryForSelect categoryForSelect in this.CategoryList)
			{
				foreach (FamilyForSelect familyForSelect in categoryForSelect.FamilyList)
				{
					familyForSelect.Family.Check = familyForSelect.Selected;
					familyForSelect.Family.Database.FamilyTable.Update(familyForSelect.Family);
				}
			}
			base.Close();
		}

		private void toolStripMenuItem1_Click(object sender, EventArgs e)
		{
			this.cmdSearch();
		}

		private void cmdSearch()
		{
			this.lbxFamily.Items.Clear();
			List<DbFamily> list = this.mdb.FamilyTable.SearchFamily(this.tstbSearchName.Text);
			if (list.Count > 0)
			{
				foreach (DbFamily dbFamily in list)
				{
					this.lbxFamily.Items.Add(dbFamily, dbFamily.Check);
				}
			}
			this.lbxFamily.Focus();
		}

		private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.lbxFamily.Items.Count; i++)
			{
				this.lbxFamily.SetSelected(i, true);
			}
		}

		private void 反选ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.lbxFamily.Items.Count; i++)
			{
				this.lbxFamily.SetSelected(i, !this.lbxFamily.GetItemChecked(i));
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
		}

		private void btnSelAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.lbxFamily.Items.Count; i++)
			{
				this.lbxFamily.SetItemChecked(i, true);
			}
		}

		private void btnSelChange_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.lbxFamily.Items.Count; i++)
			{
				this.lbxFamily.SetItemChecked(i, !this.lbxFamily.GetItemChecked(i));
			}
		}

		private void lbxFamily_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void lbxFamily_ItemCheck(object sender, ItemCheckEventArgs e)
		{
			if (this.lbxFamily.Items[e.Index] != null && this.lbxFamily.Items[e.Index] is FamilyForSelect)
			{
				FamilyForSelect familyForSelect = this.lbxFamily.Items[e.Index] as FamilyForSelect;
				CategoryForSelect category = familyForSelect.Category;
				if (familyForSelect != null)
				{
					if (e.NewValue == CheckState.Checked)
					{
						using (List<FamilyForSelect>.Enumerator enumerator = category.FamilyList.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								FamilyForSelect familyForSelect2 = enumerator.Current;
								if (familyForSelect2.ToString() == familyForSelect.ToString())
								{
									familyForSelect2.Selected = true;
									break;
								}
							}
							return;
						}
					}
					foreach (FamilyForSelect familyForSelect3 in category.FamilyList)
					{
						if (familyForSelect3.ToString() == familyForSelect.ToString())
						{
							familyForSelect3.Selected = false;
							break;
						}
					}
				}
			}
		}

		private void lbxFamily_MouseClick(object sender, MouseEventArgs e)
		{
			if (this.lbxFamily.SelectedItem != null)
			{
				int num = ((ListBox)sender).IndexFromPoint(e.X, e.Y);
				if (num == -1)
				{
					this.lbxFamily.ClearSelected();
				}
			}
		}

		private void lbxFamily_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (this.lbxFamily.SelectedItem != null)
			{
				int num = ((ListBox)sender).IndexFromPoint(e.X, e.Y);
				if (num == -1)
				{
					this.lbxFamily.ClearSelected();
				}
			}
		}

		private void cbxBb_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cbxBb.SelectedItem != null)
			{
				this.tvCategory.Nodes.Clear();
				this.lbxFamily.Items.Clear();
				this.mdb = (FamilyLibDatabase)this.cbxBb.SelectedItem;
				this.FillTree(this.mdb, this.tvCategory, CheckType.All);
				this.tvCategory.ExpandAll();
			}
		}

		public void FillTree(FamilyLibDatabase db, TreeView tree, CheckType checkType)
		{
			try
			{
				List<DbCategory> all = db.CategoryTable.GetAll();
				DbSubject dbSubject = db.SubjectTable.AllSubject()[0];
				TreeNode treeNode = new TreeNode(dbSubject.GetNameAndCount(checkType));
				treeNode.Name = dbSubject.Name;
				treeNode.Tag = dbSubject;
				this.SetChildNode(treeNode, all, checkType);
				tree.Nodes.Add(treeNode);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private void SetChildNode(TreeNode tn, List<DbCategory> cl, CheckType checkType)
		{
			foreach (DbCategory dbCategory in cl)
			{
				if (tn.Tag is CategoryForSelect)
				{
					if (dbCategory.ParentId == ((CategoryForSelect)tn.Tag).Category.Guid)
					{
						TreeNode treeNode = new TreeNode(dbCategory.GetNameAndCount(checkType));
						treeNode.Name = dbCategory.Name;
						CategoryForSelect categoryForSelect = new CategoryForSelect(dbCategory);
						foreach (DbFamily dbFamily in dbCategory.GetAllFamilies(false, checkType))
						{
							FamilyForSelect familyForSelect = new FamilyForSelect(dbFamily);
							familyForSelect.Selected = dbFamily.Check;
							familyForSelect.Category = categoryForSelect;
							categoryForSelect.FamilyList.Add(familyForSelect);
						}
						treeNode.Tag = categoryForSelect;
						treeNode.ImageIndex = 1;
						this.SetChildNode(treeNode, cl, checkType);
						tn.Nodes.Add(treeNode);
					}
				}
				else if (tn.Tag is DbSubject)
				{
					DbSubject dbSubject = (DbSubject)tn.Tag;
					if (dbCategory.ParentId == dbSubject.Guid)
					{
						TreeNode treeNode2 = new TreeNode(dbCategory.GetNameAndCount(checkType));
						treeNode2.Name = dbCategory.Name;
						CategoryForSelect categoryForSelect2 = new CategoryForSelect(dbCategory);
						foreach (DbFamily dbFamily2 in dbCategory.GetAllFamilies(false, checkType))
						{
							FamilyForSelect familyForSelect2 = new FamilyForSelect(dbFamily2);
							familyForSelect2.Selected = dbFamily2.Check;
							familyForSelect2.Category = categoryForSelect2;
							categoryForSelect2.FamilyList.Add(familyForSelect2);
						}
						treeNode2.Tag = categoryForSelect2;
						treeNode2.ImageIndex = 1;
						this.SetChildNode(treeNode2, cl, checkType);
						tn.Nodes.Add(treeNode2);
					}
				}
			}
		}

		private void DlgCheck_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private List<FamilyLibDatabase> mDbs;

		private FamilyLibDatabase mdb;
	}
}
